﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Communication
{
    /// <summary>
    /// 底物
    /// </summary>
    public class SubstrateControl:ControlBase
    {
        string mAS = "AS",//加底物
            mAG = "AG",//旋臂定位
            mAH = "AH",//旋臂复位
            mAZ = "AZ",//泵复位
            mAY = "AY";//吸底物
        string mGD = "GD";//吸液量
        string mGE = "GE";//加液量
        string mOutOpen = "MA22",
            mOutClose = "MW22",
            mIsloationOpen = "MA24",
            mIsloationClose = "MW24";

        public SubstrateControl()
        {
            mCanId = Can.R2_SUB_ID;
            mSubCanId = 0x52;
            mSCmd = new Dictionary<int, string>();
            mSCmd.Add(0, mAH);
            mSCmd.Add(1, mAG);
            mSCmd.Add(2, mAS);
            mSCmd.Add(3, mAZ);
            mSCmd.Add(4, mAY);

        }
        /// <summary>
        /// 旋臂复位 0
        /// </summary>
        /// <returns></returns>
        public int ArmReset()
        {
            SendCmd(0,mAH);
            mStatus = -100;
            return 0;
        }
        /// <summary>
        /// 旋臂定位 1
        /// </summary>
        /// <returns></returns>
        public int ArmPosition()
        {
            SendCmd(1,mAG);
            mStatus = -1;
            return 1;
        }
        /// <summary>
        /// 加底物 
        /// </summary>
        /// <returns>2</returns>
        public int ArmAddSubstrate()
        {
            Thread.Sleep(300);//等一会儿，防止倒吸
            IsloationOpen(false);
            SendCmd(2,mAS);
            mStatus = -2;
            return 2;
        }
        /// <summary>
        /// 加底物
        /// </summary>
        /// <param name="count">步数</param>
        /// <returns>2</returns>
        public int ArmAddSubstrate(int count)
        {
            Thread.Sleep(300);//等一会儿，防止倒吸
            IsloationOpen(false);
            Can.SendCan(mSubCanId, "1" + mGE + count + Can.strEnd);
            SendCmd(2, mAS);
            mStatus = -2;
            return 2;
        }

        /// <summary>
        /// 泵复位
        /// </summary>
        /// <returns></returns>
        public int PumpReset()
        {
            SendCmd(3,mAZ);
            mStatus = -3;
            return 3;
        }
        /// <summary>
        /// 吸底物，收到返回后再接收位置关掉隔离阀
        /// </summary>
        /// <returns></returns>
        public int PumpCharge()
        {
            IsloationOpen(true);
            Thread.Sleep(200);
            Can.SendCan(mSubCanId, "1" + mGD + 3110 + Can.strEnd);
            SendCmd(4,mAY);
            mStatus = -4;
            return 4;
        }
        /// <summary>
        /// 吸底物,收到返回后再接收位置关掉隔离阀
        /// </summary>
        /// <param name="count">步数</param>
        /// <returns></returns>
        public int PumpCharge(int count)
        {
            IsloationOpen(true);
            Thread.Sleep(200);
            Can.SendCan(mSubCanId,"1"+mGD+count+Can.strEnd);
            SendCmd(4,mAY);
            mStatus = -4;
            return 4;
        }
        /// <summary>
        /// 隔离阀开关
        /// </summary>
        /// <param name="open"></param>
        public void IsloationOpen(bool open)
        {
            if (open)
                Can.SendCan(Can.IO_CanID, mIsloationOpen + Can.strEnd);
            else
                Can.SendCan(Can.IO_CanID, mIsloationClose + Can.strEnd);
        }
        /// <summary>
        /// 底物吸液开关
        /// </summary>
        /// <param name="open"></param>
        public void OutOpen(bool open)
        {
            if (open)
                Can.SendCan(Can.IO_CanID, mOutOpen + Can.strEnd);
            else
                Can.SendCan(Can.IO_CanID, mOutClose + Can.strEnd);
        }
        /// <summary>
        /// 排出底物残留
        /// </summary>
        public void ArmOut()
        {
            OutOpen(true);
            Thread.Sleep(1000);
            OutOpen(false);
        }
    }
}
